<?php
namespace app\home\controller;
use \think\Validate;
use \think\captcha\Captcha;

class Shop extends \think\Controller
{
    public function shopcart()
    {
    	$user_id = session("user_id");
		$result  = db("user")->where("user_id = {$user_id}")->find();
        $data = db('shopcar')->alias('s')->join('user u','s.shopcar_uid=u.user_id')->join('good g','s.shopcar_gid=g.good_id')->field('g.*,s.good_num,s.good_countprice,s.shopcar_gid')->where("shopcar_uid={$user_id}")->select();
        $total_price = db('shopcar')->sum("good_countprice");
        $this->assign("data",$data);
		$this->assign("result",$result);
		$this->assign("user_id",$user_id);
        $this->assign("total_price",$total_price);
	    return view('shopcart');
    }

    public function addshop()
    {
        $user_id = session("user_id");
        $good_id = input('get.id');   
        $good_num = input('post.good_num');
        $ret = db('good')->where("good_id={$good_id}")->find();
        $good_price = $ret['good_price'];
        $good_countprice = $good_price*$good_num;
        $result = db('shopcar')->where("shopcar_uid={$user_id} && shopcar_gid={$good_id}")->find();
        if(!$result)
        {
            $data = 
            [
                'shopcar_uid'     =>$user_id,
                'shopcar_gid'     =>$good_id,
                'good_num'        =>$good_num,
                'good_price'      =>$good_price,
                'good_countprice' =>$good_countprice
            ];    
            $ret = db('shopcar')->insert($data);     
        }
            else
            {
                $good_num1 = $result['good_num'];
                $data =
                [
                    'good_num'    =>$good_num1+$good_num
                ];
            $ret = db('shopcar')->where("shopcar_uid={$user_id} && shopcar_gid={$good_id}")->update($data);  
            }

        if($ret ==false)
        {
            $this->error("添加失败！");
        }
        else
        {
            $this->success("成功加入购物车！");    
        }
    }

    public function delshop()
    {
        $good_id = input('get.id');
        $ret = db('shopcar')->where("shopcar_gid={$good_id}")->delete();
        if( $ret ==false )
        {
            $this->error("在购物车中删除商品失败",'/home/shop/shopcart');
        }
        $this->success("在购物车中删除商品成功",'/home/shop/shopcart');
    }

    public function cutgoodnum()
    {
        $user_id = session("user_id");
        $good_id = input('get.good_id');
        $countprice = input('get.countprice');
        $good_num = input('get.num');
        $total_price = input('get.total_price');
        $data =
        [
            'good_num'           =>$good_num,
            'good_countprice'    =>$countprice
        ];
        $ret = db('shopcar')->where("shopcar_gid",$good_id)->update($data);
        $total_price = db('shopcar')->where("shopcar_uid",$user_id)->sum("good_countprice");
         if($ret ==false)
        {
            $this->error("更新数量失败！");
        }
        else
        {
         return 
         ['code'=>1,'msg'=>"更新成功",'num'=>$good_num,'countprice'=>$countprice,'total_price'=>$total_price];
        }
    }

    public function addgoodnum()
    {
        $user_id = session("user_id");
        $good_id = input('get.good_id');
        $countprice = input('get.countprice');
        $good_num = input('get.num');
        $total_price = input('get.total_price');
        $data =
        [
            'good_num'           =>$good_num,
            'good_countprice'    =>$countprice
        ];
        $ret = db('shopcar')->where("shopcar_gid",$good_id)->update($data);
        $total_price = db('shopcar')->where("shopcar_uid",$user_id)->sum("good_countprice");
        //echo $total_price;
         if($ret ==false)
        {
            return 
            ['code'=>0,'msg'=>"更新数量失败！"];
        }
        else
        {
         return 
         ['code'=>1,'msg'=>"更新成功",'num'=>$good_num,'countprice'=>$countprice,'total_price'=>$total_price];
        }
    }

    public function make_order()
    {
        $user_id = input('get.user_id');
        $gid_str = input('get.gid_str');
        $gnum_str = input('get.gnum_str');
        $total_price = input('get.totalprice');
        //echo($gid_str);
         if($total_price==0)
        {
            return 
            ['code'=>0,'msg'=>"无选择任何商品！"];
        }
        else
        {
             $data =
            [
                'order_uid'    =>$user_id,
                'order_time'   =>time(),
                'order_gid'    =>$gid_str,
                'order_gnum'   =>$gnum_str,
                'order_price'  =>$total_price
            ];
           $ret = db('order')->insert($data);
           $neworder_id = db('order')->getLastInsID();
           return 
         ['code'=>1,'msg'=>"已成功生成订单",'neworder_id'=>$neworder_id];
        }
    }    

    public function order_rightnow()
    {
        $user_id = input('get.user_id');
        $order_price = input('get.order_price');
        $good_num = input('get.good_num');
        $good_id = input('get.good_id');
        //echo($gid_str);
         if($order_price==0)
        {
            return 
            ['code'=>0,'msg'=>"无选择任何商品！"];
        }
        else
        {
             $data =
            [
                'order_uid'    =>$user_id,
                'order_gid'    =>$good_id,
                'order_time'   =>time(),
                'order_gnum'    =>$good_num,
                'order_price'  =>$order_price
            ];
           $ret = db('order')->insert($data);
           $neworder_id = db('order')->getLastInsID();
           return 
         ['code'=>1,'msg'=>"已成功生成订单",'neworder_id'=>$neworder_id];
        }
    }    

    public function pay()
    {   header("Content-Type:text/html;charset=utf8");
        $user_id = session("user_id");   
        $order_id = $_GET['id'];
        $result = db("user")->where("user_id = {$user_id}")->find(); 
        $ret = db('address')->where("address_uid={$user_id}")->select();
        foreach ($ret as $val) {
            $address_idarr[] = $val['address_id'];  //address表里地址内容的数组
        }
        $ret2 = db('address')->where("address_uid={$user_id} && address_isdefault=1")->select();
        $result1 = db("order")->alias('o')->where("order_id={$order_id}")
        ->join('good g','o.order_gid=g.good_id')->select();
         foreach ($result1 as $val) {
            $gid_arr = explode(',', $val['order_gid']);  //order表里商品id的数组
            $gnum_arr = explode(',', $val['order_gnum']);  //order表里商品num的数组
        }
        for($a=0;$a<count($gid_arr);$a++){
        $result2[] = db("good")->where("good_id={$gid_arr[$a]}")->find();
        $result2[$a]['good_num'] = $gnum_arr[$a];
        $result2[$a]['good_totalprice'] = $result2[$a]['good_num'] * $result2[$a]['good_price'];
        } 
        $order_price = $result1[0]['order_price'];
        $order_remark = $result1[0]['order_remark'];
        $order_status = $result1[0]['order_status'];
        $order_gnum = $result1[0]['order_gnum'];
        $order_gid = $result1[0]['order_gid'];
        $this->assign("address_idarr",$address_idarr);
        $this->assign("order_price",$order_price);
        $this->assign("order_remark",$order_remark);
        $this->assign("order_status",$order_status);
        $this->assign("order_gnum",$order_gnum);
        $this->assign("order_gid",$order_gid);
        $this->assign("result1",$result1);
        $this->assign("result2",$result2);
        $this->assign("result",$result);
        $this->assign("ret",$ret);
        $this->assign("ret2",$ret2);
        $this->assign("user_id",$user_id);
        $this->assign("order_id",$order_id);
        return view('pay');
    }    

    public function add_address()
    {
        $order_id = $_GET['id'];
        $address_uid     = session("user_id");
        $address_uname   = input('post.address_uname');
        $address_tell    = input('post.address_tell','','strip_tags');
        $address_content = input('post.address_content');
        $data = [
            'address_uid'      => $address_uid,
            'address_uname'    => $address_uname,
            'address_tell'     => $address_tell,
            'address_content'  => $address_content
        ];
        $validate = validate('Address');
        if(!$validate->check($data)){
         $this->error($validate->getError());
        }
        $ret = db('address')->insert($data);  //插入数据到数据表user
        if($ret =='false')
        {
            $this->error("地址添加失败！");
        }
        $this->success("地址添加成功！");
    }

    public function change_address()
    {
        $address_id = input('get.id');
        $ret = db('address')->where("address_isdefault=1")->update(['address_isdefault' => '0']);
        if( $ret ==false )
        {
            $this->error("修改失败");
        }
        $ret1 = db('address')->where("address_id={$address_id}")->update(['address_isdefault' => '1']);
        if( $ret1 ==false )
        {
            $this->error("修改失败");
        }
        $this->success("修改成功"); 
    }

    public function deladdress()
    {
        $address_id = input('get.id');
        $ret = db('address')->where("address_id={$address_id}")->delete();
        if( $ret ==false )
        {
            $this->error("删除失败");
        }
        $this->success("删除成功"); 
    }

    public function change_order()
    {
        $order_id     = input('get.order_id');
        $order_remark = input('get.order_remark');
        $order_delivery = input('get.order_delivery');
        $data =
        [
            'order_remark'      =>$order_remark,
            'order_delivery'    =>$order_delivery
        ];
        $ret = db('order')->where("order_id={$order_id}")->update($data);
        $ret1 = db('order')->where("order_id={$order_id}")->find();
        if( $ret ==false )
        {
            if($ret1['order_delivery']==$order_delivery)    
            {
                return
                ['code'=>1,'msg'=>"同样的快递方式",'order_id'=>$order_id];
            }
        return 
         ['code'=>0,'msg'=>"更新订单失败"];
        }
        return 
         ['code'=>1,'msg'=>"成功更新订单",'order_id'=>$order_id];
    }

}
